package cn.alcatraz.org.controller;

import cn.alcatraz.basic.utils.AjaxResult;
import cn.alcatraz.basic.utils.PageList;
import cn.alcatraz.org.domain.Department;
import cn.alcatraz.org.query.DepartmentQuery;
import cn.alcatraz.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /**
    * @Title: loadAll
    * @Description: 获取全部部门数据
    * @Author: TinyYu
    * @Version: 1.0
    * @Date:  2022/6/24 19:42
    * @Parameters:
    * @Return
    */
    @GetMapping
    public List<Department> loadAll() {
        return departmentService.loadAll();
    }

    /**
    * @Title: tree
    * @Description: 获取无限极树
    * @Author: TinyYu
    * @Version: 1.0
    * @Date:  2022/6/27 14:37
    * @Parameters:
    * @Return
    */
    @GetMapping("/tree")
    public List<Department> tree() {
        return departmentService.tree();
    }

    /**
    * @Title: loadById
    * @Description: 通过id获取部门数据
    * @Author: TinyYu
    * @Version: 1.0
    * @Date:  2022/6/24 19:43
    * @Parameters:
    * @Return
    */
    @GetMapping("/{id}")
    public Department loadById(@PathVariable("id") Long id) {
        return departmentService.loadById(id);
    }

    /**
    * @Title: remove
    * @Description: 通过id删除部门数据
    * @Author: TinyYu
    * @Version: 1.0
    * @Date:  2022/6/24 19:46
    * @Parameters:
    * @Return
    */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id) {
        try {
            departmentService.removeById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试!");
        }
    }

    /**
    * @Title: edit
    * @Description: 部门添加或修改数据
    * @Author: TinyYu
    * @Version: 1.0
    * @Date:  2022/6/24 19:55
    * @Parameters:
    * @Return
    */
    @PutMapping
    public AjaxResult edit(@RequestBody Department department) { // @RequestBody：将请求body中的json数据转换为对象
        try {
            if (department.getId() == null) {
                departmentService.save(department);
            } else {
                departmentService.updateById(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试!");
        }
    }

    /**
    * @Title: query
    * @Description: 高级查询和分页数据
    * @Author: TinyYu
    * @Version: 1.0
    * @Date:  2022/6/24 19:56
    * @Parameters:
    * @Return
    */
    @PostMapping
    public PageList<Department> query(@RequestBody DepartmentQuery departmentQuery) { // @RequestBody：将请求body中的json数据转换为对象
        return departmentService.queryT(departmentQuery);
    }

    /**
    * @Title: patchRemove
    * @Description: 批量删除
    * @Author: TinyYu
    * @Version: 1.0
    * @Date:  2022/6/25 11:26
    * @Parameters:
    * @Return
    */
    @PatchMapping
    public AjaxResult patchRemove(@RequestBody Long[] ids) {
        try {
            System.out.println(ids);
            departmentService.patchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试!");
        }
    }
}
